<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<title>个人中心</title>
		<link rel="stylesheet" type="text/css" href="css/gerenzhongxin.css"/>
		<link rel="stylesheet" type="text/css" href="layui/css/layui.css"/>
		<script src="layui/layui.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script>

	</head>
	<body>
	<div class="form-wrapper">
		<form class="layui-form" action="http://user.xungeng.com/person/update" method="post" enctype="multipart/form-data">
			<div class="layui-upload">
				<label class="layui-form-label">头像</label>
			  <div class="layui-upload-list" >
<!--			    <img class="layui-upload-img" id="img1" style="width: 100px; height: 100px;">-->
				<img th:src="${user.img}" class="layui-upload-img" style="width: 100px; height: 100px;">
				</div>
				<div class="layui-upload">
					<label class="layui-form-label"></label>
					<input type="file" class="layui-btn layui-btn-xs" id="img" name="image" accept="image/*" onchange="uploadFile(this)">
					<button type="reset" class="layui-btn layui-btn-primary layui-btn-xs">重置</button>
					<p id="demoText"></p>
					<div id="find"></div>
				</div>

			  </div>
		    <div class="layui-form-item">
		        <label class="layui-form-label">ID</label>
		        <div class="layui-input-inline">
		            <input th:value="${user.id}" type="text" name="id" id="userId" required  lay-verify="required" class="layui-input layui-btn-disabled"/>
		        </div>
		    </div>
		    <div class="layui-form-item">
		        <label class="layui-form-label">姓名</label>
		        <div class="layui-input-inline">
		            <input th:value="${user.userName}" type="text" id="realName" name="realName"  required  lay-verify="required" class="layui-input layui-btn-disabled" />
		        </div>
		    </div>
		
		<div class="layui-form-item">
		    <label class="layui-form-label">用户名</label>
		    <div class="layui-input-inline">
		        <input th:value="${user.realName}" type="text" id="username" name="userName"  required  lay-verify="required" class="layui-input layui-btn-disabled" />
		    </div>
		</div>

			<div class="layui-form-item">
				<label class="layui-form-label">账号类型</label>
				<div class="layui-input-inline">
					<input value="1" type="text" id="authority" name="authority"  required  lay-verify="required" class="layui-input layui-btn-disabled" />
				</div>
			</div>
		
		    <div class="layui-form-item">
		        <label class="layui-form-label">性别</label>
		        <div class="layui-input-inline">
		            <input th:value="${user.sex==1?'男':'女'}" type="text" id="sex" name="sex" required  lay-verify="required" class="layui-input " />
		        </div>
		    </div>

			<div class="layui-form-item">
			    <label class="layui-form-label">密码</label>
			    <div class="layui-input-inline">
			        <input value="********" type="password" id="password" name="password" required  lay-verify="required" class="layui-input " />
			    </div>
			</div>
			

		    <div class="layui-form-item">
		        <label class="layui-form-label">电话</label>
		        <div class="layui-input-inline">
		            <input th:value="${user.tel}" type="text" id="tel" name="tel" required  lay-verify="required" class="layui-input " />
		        </div>
		    </div>
		    <div class="layui-form-item">
		        <label class="layui-form-label">邮箱</label>
		        <div class="layui-input-inline">
		            <input th:value="${user.email}" type="email" id="email" name="email" required  lay-verify="required" class="layui-input " />
		        </div>
		    </div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">清空</button>
				</div>
			</div>

		</form>
	</div>
<!--		<script>-->
<!--			var upload = layui.upload-->
<!--			//Demo-->
<!--			layui.use('form', function(){-->
<!--				var form = layui.form;-->
<!--				var laydate=layui.laydate;-->

<!--				laydate.render({-->
<!--					elem:'#birthday',-->
<!--					type:'datetime'-->
<!--				});-->
<!--				//监听提交-->
<!--				form.on('submit(formDemo)', function(data){-->
<!--					layer.msg(JSON.stringify(data.field));-->
<!--					return false;-->
<!--				});-->

<!--			});-->



<!--			layui.use('jquery',function () {-->
<!--				var $=layui.jquery;-->
<!--				form.on('submit(formDemo)',function (data) {-->
<!--					$.ajax({-->
<!--						url:"http://localhost:8080/api/user/setUserInformation",-->
<!--						method:"PUT",-->
<!--						data:$('#infoSubmit').serialize(),-->
<!--						success:function(res){-->
<!--							if (res.code==200){-->
<!--								layer.msg("修改成功",{time:1000})-->
<!--							}else{-->
<!--								layer.msg("修改失败",{time:1000})-->
<!--							}-->
<!--						},-->
<!--						error:function(){-->
<!--							layer.alert("发生异常！请重试")-->
<!--						}-->
<!--					})-->
<!--					return false;-->
<!--				});-->
<!--			});-->


<!--			var $ = layui.jquery-->
<!--					,upload = layui.upload-->
<!--					,element = layui.element-->
<!--					,layer = layui.layer-->
<!--					,form = layui.form;-->
<!--			//监听提交-->

<!--			form.on('submit(formDemo)', function(data){-->
<!--				layer.msg(JSON.stringify(data.field));-->
<!--				return false;-->
<!--			});-->
<!--			form.on('submit(formDemo2)', function(data){-->
<!--				layer.msg(JSON.stringify(data.field));-->
<!--				return false;-->
<!--			});-->
<!--			//常规使用 - 普通图片上传-->
<!--			var uploadInst = upload.render({-->
<!--				elem: '#img'-->
<!--				,url: '/api/user/getUserImage?file=1-201016155I5.jpg&username=admin' //改成您自己的上传接口-->
<!--				,before: function(obj){-->
<!--					//预读本地文件示例，不支持ie8-->
<!--					obj.preview(function(index, file, result){-->
<!--						$('#img1').attr('src', result); //图片链接-->
<!--					});-->

<!--				}-->
<!--				,done: function(res){-->
<!--					//如果上传失败-->
<!--					if(res.code != 200){-->
<!--						return layer.msg('上传失败');-->
<!--					}-->
<!--					//上传成功的一些操作-->
<!--					//……-->
<!--					return layer.msg("上传成功")-->
<!--					// $('#demoText').html(''); //置空上传失败的状态-->
<!--				}-->
<!--				,error: function(){-->
<!--					//演示失败状态，并实现重传-->
<!--					var demoText = $('#demoText');-->
<!--					demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');-->
<!--					demoText.find('.demo-reload').on('click', function(){-->
<!--						uploadInst.upload();-->
<!--					});-->
<!--				}-->
<!--			});-->

<!--		</script>-->

<!--	<script>-->
<!--		$(function(){-->
<!--		    $.ajax({-->
<!--				url:"/api/user/selectUserByUserName/admin",-->
<!--				method:"GET",-->
<!--				success:function (res) {-->
<!--					$("#userId").val(res.data.userId)-->
<!--					$("#realName").val(res.data.realName)-->
<!--					$("#sex").val(res.data.sex)-->
<!--					$("#username").val(res.data.username)-->
<!--					$("#password").val(res.data.password)-->
<!--					$("#authority").val(res.data.authority)-->
<!--					$("#tel").val(res.data.tel)-->
<!--					$("#email").val(res.data.email)-->
<!--                }-->
<!--			})-->
<!--		});-->

<!--	</script>-->
	<script>
		function uploadFile(input) {
			console.log("input的值为 " + input.files[0]);
			const file = input.files[0];

			// 创建一个FormData对象
			const formData = new FormData();
			formData.append("file", file, file.name);

			// 创建一个新的XMLHttpRequest对象
			const xhr = new XMLHttpRequest();

			// 创建请求
			xhr.open("POST", "http://user.xungeng.com/update/img", true);

			// 设置请求头
			xhr.setRequestHeader("enctype", "multipart/form-data");
			xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");

			// 监听事件
			xhr.onreadystatechange = function () {
				if (xhr.readyState === 4) {
					if (xhr.status === 200) {
						// 文件上传成功的逻辑
						location.reload();
					} else {
						// 文件上传失败的逻辑
					}
				}
			};

			// 发送请求
			xhr.send(formData);
		}
	</script>
	</body>
</html>

